package com.coollang.smartbra.presenter;

import com.coollang.smartbra.activity.DeviceInfoActivity;
import com.coollang.smartbra.model.DeviceInfoModel;
import com.coollang.smartbra.model.DeviceModel;

import java.util.HashMap;
import java.util.Map;
import io.realm.Realm;

/**
 * @Auther: Timmy
 * @Data: 2017/7/13
 * @Fuction:
 */

public class DeviceInfoActivityPresenter extends BasePresenter<DeviceInfoModel> {
    private DeviceInfoActivity mInfoActivity;

    public DeviceInfoActivityPresenter(DeviceInfoActivity infoActivity) {
        super(DeviceInfoModel.class);
        mInfoActivity = infoActivity;
    }

    @Override
    public void setRequestInfo() {
    }

    @Override
    protected Map<String, String> getData() {
        return mMap;
    }

    @Override
    protected void parsrData(DeviceInfoModel model) {

        int code = model.getCode();
        if (code == 200) {
            if (requestCode == 2) {
                // 删除数据库
                deleteSql();
                mInfoActivity.unBindSucceed();
            }
        }
    }

    private void deleteSql() {
        mRealm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                realm.delete(DeviceModel.class);
            }
        });
    }

    private Map<String, String> mMap;

    private int requestCode;

    public void unBindDevice(String mac) {
        requestCode = 2;
        // 解除绑定设备
        mMap = new HashMap<>();
        mMap.put("mac", mac.replace(":",""));
        setBaseOkgo("device/unbinding-device");
    }
}
